package pl.edu.agh.portal.application.portlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletConfig;
import javax.portlet.PortletException;
import javax.portlet.PortletMode;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import pl.edu.agh.portal.application.types.Message;
import pl.edu.agh.portal.application.utils.PortletsUtils;

public class MessageCreatePortlet extends ThesisGenericPortlet {

	private static final String NORMAL_VIEW = "/message_create_portlet.jsp";
	
	private PortletRequestDispatcher normalView;
	
	private String[] errorList = new String[0];
	private Message temporaryMessage;
	
	public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
		
		if (errorList.length > 0) {			
			request.setAttribute(PortletsUtils.ATTRIBUTE_ERROR_MESSAGES, errorList);
		} else {
			temporaryMessage = new Message();
		}
		request.setAttribute(PortletsUtils.ATTRIBUTE_MESSAGE_FOR_CREATION, temporaryMessage);
		normalView.include(request, response);
	}
	
	@Override
	public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
		
		String value = request.getParameter(PortletsUtils.MESSAGE_CREATE_KEY);
		if (value == null)	value = "";
		if (value.equals(PortletsUtils.BUTTON_CANCEL)) {
			errorList = new String[0];
			response.sendRedirect("/portal/portal/message_list.psml");
		} else if (value.equals(PortletsUtils.BUTTON_SEND)) {
			errorList = validate(request);
			if (errorList.length == 0) {
				//TODO: WS add(session, temporaryPorject)
				temporaryMessage.setId(1);
				response.sendRedirect("/portal/portal/message_list.psml?mode=2");
			}			
		} else {
			temporaryMessage = new Message();
			value = request.getParameter(PortletsUtils.MESSAGE_MENU_KEY);
			if (value == null)	value = "";			
			if (value.equals(PortletsUtils.BUTTON_CREATE_MESSAGE)) {
				response.sendRedirect("/portal/portal/message_create.psml");
			} else if (value.equals(PortletsUtils.BUTTON_INBOX_MESSAGES)) {
				response.sendRedirect("/portal/portal/message_list.psml?mode=1");
			} else if (value.equals(PortletsUtils.BUTTON_OUTBOX_MESSAGES)) {
				response.sendRedirect("/portal/portal/message_list.psml?mode=2");
			} else {
				response.setPortletMode(PortletMode.VIEW);
			}
		}
	}	
	
	public void init(PortletConfig config) throws PortletException {
		super.init(config);
		normalView = config.getPortletContext().getRequestDispatcher(NORMAL_VIEW);
	}
	
	public void destroy() {
		normalView = null;
		super.destroy();
	}

	private String[] validate(ActionRequest request) {
		ArrayList<String> errorList = new ArrayList<String>();
		
		String parameterMessageTitle = request.getParameter(PortletsUtils.PARAMETER_MESSAGE_TITLE);
		String parameterMessageContent = request.getParameter(PortletsUtils.PARAMETER_MESSAGE_CONTENT);
		String parameterMessageDestination = request.getParameter(PortletsUtils.PARAMETER_MESSAGE_DESTINATION);
				
		if (parameterMessageTitle != null && parameterMessageTitle.trim().equals("")) {
			errorList.add(PortletsUtils.ERROR_PARAMETER_MESSAGE_TITLE);
		} else {
			temporaryMessage.setTitle(parameterMessageTitle);
		}
		if (parameterMessageContent != null && parameterMessageContent.trim().equals("")) {			
			errorList.add(PortletsUtils.ERROR_PARAMETER_MESSAGE_CONTENT);
		} else {
			temporaryMessage.setMessage(parameterMessageContent);
		}
		if (parameterMessageDestination != null && parameterMessageDestination.trim().equals("")) {			
			errorList.add(PortletsUtils.ERROR_PARAMETER_MESSAGE_DESTINATION);
		} else {
			temporaryMessage.setDestination(parameterMessageDestination);
		}		
		
		return errorList.toArray(new String[0]);
	}
	
}
